package cn.com.frame.util.date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 *
 * //org.apache.commons.lang3.time.DateUtils  日期增加工具类
 *
 *
 */
public class DateUtil {
    private static final FastDateFormat DATETIME_FORMAT_YMDHMS = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    private static final FastDateFormat DATETIME_FORMAT_YMDHM = FastDateFormat.getInstance("yyyy-MM-dd HH:mm");

    /**
     *
     * @param strDate
     * @return  yyyy-MM-dd HH:mm:ss
     */
    public static Date parseyMdHms(String strDate){

        try {
            return  DATETIME_FORMAT_YMDHMS.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *
     * @param strDate
     * @return yyyy-MM-dd
     */
    public static Date parseyMd(String strDate){
        try {

            return  DateFormatUtils.ISO_DATE_FORMAT.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String formatyMdHms(Date date){
        return DATETIME_FORMAT_YMDHMS.format(date);
    }
    public static String formatyMdHms(long millis){
        return DATETIME_FORMAT_YMDHMS.format(millis);
    }

    public static String formatyMdHm(Date date){
        return DATETIME_FORMAT_YMDHM.format(date);
    }
    public static String formatyMdHm(long millis){
        return DATETIME_FORMAT_YMDHM.format(millis);
    }

    public static String formatyMd(Date date){
        return DateFormatUtils.ISO_DATE_FORMAT.format(date);
    }
    public static String formatyMd(long millis){
        return DateFormatUtils.ISO_DATE_FORMAT.format(millis);
    }

    /**
     *  ChronoUnit.DAYS.between(s, e);
     *
     * 比较字符串日期格式的差
     * @param startTime
     * @param endTime
     * @param type  exp: ChronoUnit.DAYS
     * @return
     */
    public static long timeDifference(String startTime,String endTime,ChronoUnit type){
        String sType = type.toString();
        if("Hours".equals(sType) || "Minutes".equals(sType) || "Seconds".equals(sType)){

            return timeDifferenceHMm(parseyMdHms(startTime),parseyMdHms(endTime),sType);
        }
        LocalDate s = LocalDate.parse(startTime);
        LocalDate e = LocalDate.parse(endTime);
        return type.between(s,e);
    }

    /**
     *  ChronoUnit.DAYS.between(s, e);
     *
     * 比较日期的差
     * @param startTime
     * @param endTime
     * @param type  exp: ChronoUnit.DAYS
     * @return
     */
    public static long timeDifference(Date startTime,Date endTime,ChronoUnit type){
        String sType = type.toString();
        if("Hours".equals(sType) || "Minutes".equals(sType) || "Seconds".equals(sType)){

            return timeDifferenceHMm(startTime,endTime,sType);
        }
        LocalDate s = startTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate e = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return type.between(s,e);

    }

    private static long timeDifferenceHMm(Date startTime,Date endTime,String type){

        Long e = endTime.getTime();
        Long s = startTime.getTime();

        if("Hours".equals(type)){
            return (e/(1000*60*60))-(s/(1000*60*60));
        }else if("Minutes".equals(type)){
            return (e/(1000*60))-(s/(1000*60));
        }else if("Seconds".equals(type)){
            return (e/1000)-(s/1000);
        }else{ //毫秒
            return e - s;
        }

    }

    public static void main(String[] args) throws ParseException {
        String dateStr = "2018-01-12 17:12:34";

        System.out.println(parseyMd(dateStr));




        String dateEnd = "2018-01-12 19:12:34";

       /* long days = timeDifference(dateStr,dateEnd,ChronoUnit.MINUTES);
        System.out.println(days);*/



        Date s = parseyMdHms(dateStr);
        Date e = parseyMdHms(dateEnd);
        long days = timeDifference(s,e,ChronoUnit.MINUTES);
        System.out.println(days);
        //org.apache.logging.log4j.core.util.datetime.FastDateFormat;
        //org.apache.commons.lang3.time.DateUtils


        System.out.println(ChronoUnit.HOURS.toString());
        System.out.println(ChronoUnit.HOURS.name());



    }

}
